
$(function () {
    findById();
})

/**
 * 查询id
 */
function findById() {
    let id = sessionStorage.getItem("productId");
    let result = myAjax("/admin/product/findById", {id: id},"post");
    console.log(result);
    if (result != null) {
        setData(result.model);
    }

}

//赋值
function setData(data) {
    $("#houseId option[value='"+data.houseName+"']").attr("selected",true);
    $("#productName").val(data.name);
    // $("#example").val(data.example);
    $("#marketPrice").val(data.marketPrice);
    $("#normalPrice").val(data.normalPrice);
    $("#showImg").attr("src",data.img);
    // $("#showImg1").attr("src",data.navImg);
    $("input[name='status'][value=" + data.status + "]").attr("checked", true);
    // $("#description").html(data.description);
    edit.txt.html(data.description);


}


function goBack() {
    $('.right').load('/html/admin/product/productManage.html');
}

//提交表单时再一次判断提交数据是否正确
function save() {
    let data = new FormData();
    data.append("id",sessionStorage.getItem("productId"));
    data.append("houseId", $("#houseId").val());
    data.append("name", $("#productName").val());
    data.append("marketPrice", $("#marketPrice").val());
    data.append("normalPrice", $("#normalPrice").val());
    // data.append("example", $("#example").val());
    // data.append("isShow", $("input:radio[name='isShow']:checked").val());
    data.append("status",$('input:radio[name="status"]:checked').val());
    //拿到的图片有可能是一组数据，[0].files[0]选中当前对象的第一个数据
    data.append("img", $("#img")[0].files[0]);
    data.append("description",edit.txt.html());
    let result = myImgAjax("/adminFile/product/update", data,"post");
    console.log(result);
    if(result.result == "success"){
        alert("修改成功,返回查询界面（== ^^ ==）！")
        $(".right").load("/html/admin/product/productManage.html");
    }else{
        alert("修改失败！");
    }
}